package com.shsc.seasar.search.ui;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.ui.ISearchPage;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.FileTextSearchScope;
import org.eclipse.search.ui.text.TextSearchQueryProvider.TextSearchInput;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IWorkingSet;

/**
 * @author lis4i0
 *
 */
public class SeasarSearchPage extends DialogPage implements ISearchPage {


    
	private ISearchPageContainer fContainer;
	
	private ISearchTab sqlSearchTab = null;
	
	//Table Modified Search Control
	private TabFolder tab;	
	
	/* (non-Javadoc)
	 * @see org.eclipse.search.ui.ISearchPage#performAction()
	 */
	public boolean performAction() {
		try {
            NewSearchUI.runQueryInBackground(sqlSearchTab.newQuery());
        } catch (CoreException e) {
            ErrorDialog.openError(getShell(), SearchMessages.TextSearchPage_replace_searchproblems_title,
            		SearchMessages.TextSearchPage_replace_searchproblems_message, e.getStatus());
            return false;
        }
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.search.ui.ISearchPage#setContainer(org.eclipse.search.ui.ISearchPageContainer)
	 */
	public void setContainer(ISearchPageContainer arg0) {
		fContainer= arg0;
	}
	
	/**
	 * @return
	 */
	public ISearchPageContainer getContainer() {
		return fContainer;
	}

	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite arg0) {
		initializeDialogUnits(arg0);
		
		//
		Composite result= new Composite(arg0, SWT.NONE);
		result.setFont(arg0.getFont());
        result.setLayout(new FormLayout());
		addTabFolderControl(result);
		setControl(result);
		Dialog.applyDialogFont(result);
	}
	
	/**
	 * @param group
	 */
	private TabFolder addTabFolderControl(Composite group){
		if (tab != null) return tab;
		tab = new TabFolder(group, SWT.NONE);
		tab.setBackground(group.getBackground());
		tab.setForeground(group.getForeground());
		tab.setFont(group.getFont());
		FormData formdata = new FormData();		
		formdata.left = new FormAttachment(0, 5);
		formdata.top = new FormAttachment(0, 5);
		formdata.right = new FormAttachment(100, -5);
		formdata.bottom = new FormAttachment(100, -5);
		tab.setLayoutData(formdata);
		
		sqlSearchTab = new SqlSearchTab(fContainer);
		sqlSearchTab.createTabItem(tab);

		TabItem item2 = new TabItem(tab, SWT.NONE);
		item2.setText("Method Search");
		Group group2 = new Group(tab, SWT.NONE);
		group2.setLayout(new GridLayout());
		item2.setControl(group2);
		return tab;
	}	
	
	public void dispose() {
		sqlSearchTab.dispose();
        super.dispose();
    }
	
	public void setVisible(boolean visible) {
		((SqlSearchTab)sqlSearchTab).setVisible(visible);
		super.setVisible(visible);
	}
}

